package us.nchu.tomtiger.dao;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import us.nchu.tomtiger.model.UserPending;
import us.nchu.tomtiger.rowMapper.UserPendingRowMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by XiongXuan on 2018/1/11.
 */
@Repository
public class UserPendingDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    public boolean insert(UserPending userPending) {
        String sql = "insert into user_pending(phone_number, changed_user_type) values(?,?);";
        Object[] objects = new Object[] {
                userPending.getPhoneNumber(),
                userPending.getChangedUserType()
        };
        return jdbcTemplate.update(sql, objects) > 0;
    }

    public List<UserPending> queryForUnchecked() {
        String sql = "select * from user_pending where checked=0;";
        return jdbcTemplate.query(sql, new UserPendingRowMapper());
    }

    public void checkAndUpdateUser(UserPending userPending, String adminId) {
        String sql = "update user_pending set checked=1, result=?, admin_id=? where id=?";
        Object[] objects = new Object[] {
                userPending.getResult(),
                adminId,
                userPending.getId()
        };

        jdbcTemplate.update(sql, objects);
    }
}
